Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| for string_index, string in enumerate(strings): | ||
| for other_string in strings[string_index+1:]: | ||
| common = find_common_prefix(string, other_string) | ||
| if len(common) > len(longest): | ||
| longest = common | ||
|
|
||
| for i in range(len(strings) - 1): | ||
| common = find_common_prefix(strings[i], strings[i + 1]) | ||
|
|
||
| if len(common) > len(longest): | ||
| longest = common | ||
|
|
There was a problem hiding this comment.
Can you use complexity to explain how the new implementation is better than the original implementation?
There was a problem hiding this comment.
Thanks of course. The original implementation compares every possible pair of strings, resulting in O(n²) pair comparisons. The updated implementation sorts the strings and compares only adjacent pairs, reducing the number of prefix comparisons to O(n). Sorting takes O(n log n) comparisons, so the overall time complexity is O(n log n + n × m), where n is the number of strings and m is the maximum string length. It also avoids repeatedly creating list slices, reducing unnecessary memory allocations. Is it OK?
There was a problem hiding this comment.
If string length,
There was a problem hiding this comment.
Thanks. The maximum string length, m, affects the cost of comparing strings because finding their common prefix can take O(m) time in the worst case. In the original implementation, there are O(n²) pairs to compare, so the overall time complexity is O(n² × m). In the updated implementation, sorting n strings takes O(n × m × log n) in the worst case because each string comparison can take O(m). Comparing adjacent strings afterwards takes O(n × m). Therefore, the overall worst-case time complexity of the updated implementation is O(n × m × log n), which is an improvement over O(n² × m). Both implementations depend on the string length m, but your new implementation reduces the number of string comparisons from quadratic growth to sorting-level growth. So I think the complexity of sorting is better compared with the original implementation
Self checklist
Task code
CYF-1183
Changelist
I do not feel very struggle for these two tasks with google help but need more practice,I think